package com.itheima.config;

import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.stereotype.Component;
import org.springframework.transaction.PlatformTransactionManager;

import javax.sql.DataSource;

@Configuration
public class JdbcConfig
{

    @Value("${jdbc.password}")
    private String password;
    @Value("${jdbc.username}")
    private String username;
    @Value("${jdbc.url}")//数据库访问地址
    private String url;
    @Value("${jdbc.driver}")//jdbc驱动类的全类名
    private String driver;

    @Bean//bean对象，DataSource类的对象，作用是数据源
        //我用的是sql数据源
    public DataSource dataSource ()
    {
        DruidDataSource dataSource=new DruidDataSource();

        dataSource.setPassword(password);
        dataSource.setUsername(username);
        dataSource.setUrl(url);
        dataSource.setDriverClassName(driver);//不是传入driver对象，而是传入driver对象的地址

        return dataSource;
    }


    @Bean//bean对象，PlatformTransactionManager类的对象，是事务管理器
    public PlatformTransactionManager transactionManager(DataSource dataSource){
        DataSourceTransactionManager ds = new DataSourceTransactionManager();
        ds.setDataSource(dataSource);//对dataSource数据源进行事务管理
        return ds;
    }

}
